package models

import (
	"github.com/beego/beego/v2/client/orm"
	"ltbz/cultural_tourism_api/utils"
	"time"
)

type Operator struct {
	OperatorId     int       `orm:"column(operator_id);pk"`
	RealName       string    `description:"姓名"`
	Mobile         string    `description:"手机号"`
	OperatorStatus string    `description:"状态:启用,禁用"`
	ScenicSpotId   int       `description:"景区id"`
	AppToken       string    `description:"token"`
	Uuid           string    `description:"UUID"`
	Password       string    `description:"密码"`
	CreateTime     time.Time `description:"创建时间"`
	ModifyTime     time.Time `description:"修改时间"`
}

type OperatorView struct {
	TradeId        int    `description:"交易ID"`
	OperatorId     int    `orm:"column(operator_id);pk"`
	RealName       string `description:"姓名"`
	Mobile         string `description:"手机号"`
	OperatorStatus string `description:"状态:启用,禁用"`
	ScenicSpotId   int    `description:"景区id"`
	AppToken       string `json:"-" description:"token"`
	Uuid           string `json:"-" description:"UUID"`
	Password       string `json:"-" description:"密码"`
	ScenicSpotName string `description:"景区名称"`
	CreateTime     string `description:"创建时间"`
	ModifyTime     string `description:"修改时间"`
}

func (obj *Operator) AddOperator() (err error) {
	o := orm.NewOrm()
	_, err = o.Insert(obj)
	return
}

type OperatorListReq struct {
	ScenicSpotId int    `description:"景区id"`
	RealName     string `description:"姓名"`
	Mobile       string `description:"手机号"`
	utils.PagingReq
}

func (r *Operator) GetOperatorPageCount(condition string, pars []interface{}) (count int, err error) {
	o := orm.NewOrm()
	sql := `SELECT COUNT(1) AS count FROM operator AS a WHERE 1=1 `
	if condition != "" {
		sql += condition
	}
	err = o.Raw(sql, pars).QueryRow(&count)
	return
}

func (r *Operator) GetScenicSpotPage(condition string, pars []interface{}, startSize, pageSize int) (items []*OperatorView, err error) {
	o := orm.NewOrm()
	sql := `SELECT a.*,b.scenic_spot_name FROM operator AS a
            LEFT JOIN scenic_spot AS b ON a.scenic_spot_id=b.scenic_spot_id WHERE 1=1 `
	if condition != "" {
		sql += condition
	}
	sql += ` ORDER BY a.create_time DESC LIMIT ?,? `
	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
	return
}

type OperatorPageResp struct {
	List   []*OperatorView
	Paging *utils.PagingResp
}

type OperatorAddReq struct {
	RealName     string `description:"姓名"`
	Mobile       string `description:"手机号"`
	ScenicSpotId int    `description:"景区id"`
	Password     string `description:"密码"`
}

func (r *Operator) GetOperatorExist(mobile string, operatorId int) (count int, err error) {
	o := orm.NewOrm()

	if operatorId > 0 {
		sql := `SELECT COUNT(1) AS count FROM operator WHERE mobile=? AND operator_id<> ? `
		err = o.Raw(sql, mobile, operatorId).QueryRow(&count)
	} else {
		sql := `SELECT COUNT(1) AS count FROM operator WHERE mobile=? `
		err = o.Raw(sql, mobile).QueryRow(&count)
	}
	return
}

// 修改
func (obj *Operator) Update(updateParams, whereParam map[string]interface{}) (err error) {
	to := orm.NewOrm()
	ptrStructOrTableName := "operator"

	qs := to.QueryTable(ptrStructOrTableName)
	for expr, exprV := range whereParam {
		qs = qs.Filter(expr, exprV)
	}
	_, err = qs.Update(updateParams)
	return
}

type OperatorEditReq struct {
	OperatorId   int    `description:"操作员id"`
	RealName     string `description:"姓名"`
	Mobile       string `description:"手机号"`
	ScenicSpotId int    `description:"景区id"`
}

type OperatorEnableOrDisableReq struct {
	OperatorId int `description:"操作员id"`
}

func (r *Operator) GetOperatorById(operatorId int) (item *OperatorView, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM operator WHERE operator_id=? `
	err = o.Raw(sql, operatorId).QueryRow(&item)
	return
}

type OperatorModifyPwdReq struct {
	OperatorId int    `description:"操作员id"`
	Pwd        string `description:"新密码"`
	ConfirmPwd string `description:"确认新密码"`
}

func (r *Operator) GetOperatorVerificationRecordsCount(condition string, pars []interface{}) (count int, err error) {
	o := orm.NewOrm()
	sql := ` SELECT COUNT(1) AS count
			FROM  operator AS a
			INNER JOIN trade AS b ON a.operator_id=b.operator_id
			INNER JOIN goods AS c ON b.goods_id=c.goods_id
			INNER JOIN wx_user AS d ON b.wx_user_id=d.wx_user_id
			LEFT JOIN scenic_spot AS e ON a.scenic_spot_id=e.scenic_spot_id
            WHERE 1=1 `

	if condition != "" {
		sql += condition
	}
	err = o.Raw(sql, pars).QueryRow(&count)
	return
}

func (r *Operator) GetOperatorVerificationRecordsList(condition string, pars []interface{}, startSize, pageSize int) (items []*OperatorVerificationRecords, err error) {
	o := orm.NewOrm()
	sql := ` SELECT b.trade_id,b.trade_code,c.goods_code,c.goods_name,d.mobile,d.wx_user_id,e.scenic_spot_name,b.verification_time
			FROM  operator AS a
			INNER JOIN trade AS b ON a.operator_id=b.operator_id
			INNER JOIN goods AS c ON b.goods_id=c.goods_id
			INNER JOIN wx_user AS d ON b.wx_user_id=d.wx_user_id
			LEFT JOIN scenic_spot AS e ON a.scenic_spot_id=e.scenic_spot_id
            WHERE 1=1 `
	if condition != "" {
		sql += condition
	}
	sql += ` ORDER BY b.verification_time DESC LIMIT ?,? `
	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
	return
}

type OperatorVerificationRecords struct {
	TradeId          int    `description:"交易id"`
	TradeCode        string `description:"交易编码"`
	WxUserId         int    `description:"用户id"`
	GoodsCode        string `description:"商品编码"`
	GoodsName        string `description:"商品名称"`
	Mobile           string `description:"手机号"`
	ScenicSpotName   string `description:"景区名称"`
	VerificationTime string `description:"核销时间"`
}

type OperatorVerificationRecordsResp struct {
	List   []*OperatorVerificationRecords
	Paging *utils.PagingResp
}

type OperationRecordsReq struct {
	OperatorId            int    `description:"操作员id"`
	TradeCode             string `description:"交易编码"`
	WxUserId              int    `description:"用户id"`
	Mobile                string `description:"手机号"`
	GoodsCode             string `description:"商品编码"`
	GoodsName             string `description:"商品名称"`
	VerificationStartTime string `description:"核销开始时间"`
	VerificationEndTime   string `description:"核销结束时间"`
	utils.PagingReq
}

type OperationRecordsExportReq struct {
	OperatorId            int    `description:"操作员id"`
	TradeCode             string `description:"交易编码"`
	WxUserId              int    `description:"用户id"`
	Mobile                string `description:"手机号"`
	GoodsCode             string `description:"商品编码"`
	GoodsName             string `description:"商品名称"`
	VerificationStartTime string `description:"核销开始时间"`
	VerificationEndTime   string `description:"核销结束时间"`
}

func (r *Operator) GetOperatorVerificationRecordsListExport(condition string, pars []interface{}) (items []*OperatorVerificationRecords, err error) {
	o := orm.NewOrm()
	sql := ` SELECT b.trade_id,b.trade_code,c.goods_code,c.goods_name,d.mobile,d.wx_user_id,e.scenic_spot_name,b.verification_time
			FROM  operator AS a
			INNER JOIN trade AS b ON a.operator_id=b.operator_id
			INNER JOIN goods AS c ON b.goods_id=c.goods_id
			INNER JOIN wx_user AS d ON b.wx_user_id=d.wx_user_id
			LEFT JOIN scenic_spot AS e ON a.scenic_spot_id=e.scenic_spot_id
            WHERE 1=1 `
	if condition != "" {
		sql += condition
	}
	sql += ` ORDER BY b.verification_time DESC `
	_, err = o.Raw(sql, pars).QueryRows(&items)
	return
}
